package threadPool.executor;

import java.util.Random;
import java.util.concurrent.*;

/**
 * Description
 *
 * @ClassName threadPool
 * @Copyright 炫彩互动
 * @Author yanglh
 * @Create Date  2016/6/24
 * @Modified by none
 * @Modified Date
 */

public class ExecutorTest {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newCachedThreadPool();
        ExecutorCompletionService executorCompletionService = new ExecutorCompletionService(executor);
        for(int i=0;i<10;i++){
            executorCompletionService.submit(new Callable() {
                @Override
                public Integer call() throws Exception {
                    Random random = new Random();
                    int i = random.nextInt(10);
                    System.out.println(Thread.currentThread().getId()+"生产了随机数："+i);
                    return i;
                }
            });
        }
        for(int i=0;i<10;i++){
            try {
                System.out.println(Thread.currentThread().getId()+"消费了随机数"+executorCompletionService.take().get());;
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
    }
}
